package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeReservationCountVo;
import io.swagger.models.auth.In;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerReservationImpl implements CustomerReservationService {
    @Autowired
    CustomerReservationMapper customerReservationMapper;

    /**
     * 查询取消预约数量
     * 总计3次
     * @return
     */
    @Override
    public Integer getCancelledCount() {

        Long id = UserThreadLocal.get();

        LocalTime beginTime = LocalTime.of(0, 0, 0);
        LocalTime endTime = LocalTime.of(23, 59, 59);
        LocalDate now = LocalDate.now();

        LocalDateTime begin=LocalDateTime.of(now,beginTime);
        LocalDateTime end=LocalDateTime.of(now,endTime);
        Integer num=customerReservationMapper.getCancelledCount(id,begin,end);

        //最多取消3次
        return num;

    }

    /**
     * 查询未来三天的预约人数
     * @return
     */
    @Override
    public List<TimeReservationCountVo> getTimeReservationCount(Long time) {
        /*************************************，通过前端传过来的当前时间戳 查询当天的次数**********************************************************/

        Instant instant = Instant.ofEpochMilli(time); // 将时间戳转换为Instant
        //当天0.00 2025-01-19T00:00
        LocalDateTime begin = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); // 转换为LocalDateTime
        LocalDateTime end=begin.plusHours(23).plusMinutes(59).plusSeconds(59);

        List<TimeReservationCountVo> TRCV =customerReservationMapper.getTimeReservationCount2(begin,end);


        /******************************下面是查询当前时间开始 未来几天的天数********************************************/
        /****************但是接口调用很频繁 每点击一天 就会调用接口 所以我们使用上面的，通过前端传过来的当前时间戳 查询当天的******************************************************/

//        //允许查询从今天开始 未来3天的 3天包括今天
//        int day=3;
//
//        //创建日期集合
//        List<LocalDateTime> timeList=new ArrayList<>();
//
//        //填入日期数据
//        for(int i=0;i<day;i++){
//            //获取今天的日期
//            LocalDate today = LocalDate.now().plusDays(i);
//
//            //获取8.00的时间
//            LocalTime localTime = LocalTime.of(8, 0);
//            LocalDateTime localDateTime = LocalDateTime.of(today, localTime);
//            //传入8.00的时间
//            timeList.add(localDateTime);
//
//            for(int j=0;j<20;j++){
//                //每加＋30分钟 就新增一个
//                localDateTime= localDateTime.plusMinutes(30);
//               timeList.add(localDateTime);
//            }
//        }
//        List<TimeReservationCountVo> TRCV=customerReservationMapper.getTimeReservationCount(timeList);
//
//        System.out.println(TRCV);
        return TRCV;
    }

    @Override
    public void addReservation(ReservationDto reservationDto) {
        Reservation reservation=new Reservation();
        BeanUtil.copyProperties(reservationDto,reservation);
        customerReservationMapper.addReservation(reservation);

    }

    @Override
    public PageResponse<ReservationVo> pageQuery(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Long id=UserThreadLocal.getUserId();
        Page<ReservationVo> pageReservation= customerReservationMapper.pageQuery(status,id);
        return PageResponse.of(pageReservation,ReservationVo.class);
    }

    @Override
    public void cancel(Integer id) {
        customerReservationMapper.cancel(id);
    }
}
